/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cratos.xxgram.platf.base;

import static org.redkale.boot.Application.RESNAME_APP_CONF;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.redkale.service.RetLabel;
import org.redkale.util.Comment;
/**
 *
 * @author zhangjx
 */
public  class MsgConstant {

	@RetLabel(" 加入了群聊!!!!")
    public static final int GROUP_JOIN = 100001;
    
	@RetLabel(" 被踢出了群聊!!!!")
    public static final int GROUP_KICK = 100002;
	
	@RetLabel(" 退出了群聊!!!!")
    public static final int GROUP_QUIT = 100003;
	
	@RetLabel("群已解散!!!")
    public static final int GROUP_DISMISS = 100004;
	
	@RetLabel("群开启了禁言!")
    public static final int GROUP_FORBIDDENWORD = 100005;
	
	@RetLabel("禁言已取消!")
    public static final int GROUP_ENABLEWORD = 100006;
	
	@RetLabel("切换匿名群成功!")
    public static final int GROUP_UPDATEANNOYTYPE = 100007;
	
	@RetLabel("切换普通群成功!")
    public static final int GROUP_UPDATENORMALTYPE = 100008;
	
	@RetLabel("更新群名称成功!")
    public static final int GROUP_UPDATENAME = 100009;
	
	@RetLabel("更新群公告成功!")
    public static final int GROUP_UPDATENOTICE = 100011;
	
	
	@RetLabel("创建群成功!!!!")
    public static final int GROUP_CREATE = 100012;
	
	@RetLabel("已经是好友了可以开始聊天!")
    public static final int FRIEND_BINDFRIEND = 200001;
	
	
    protected static Map<String, Map<Integer, String>> msgrets = RetLabel.RetLoader.loadMap(MsgConstant.class);

    @Comment("消息国际化前缀")
    private  static final  String MESSAGE_PREFIX = "sysmsg_";
    
    {
      try {
            loadProperties();
        } catch (IOException ex) {
            Logger.getLogger(MsgConstant.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    protected static final Map<Integer, String> msgdefret = msgrets.get("");


    public static String retInfo(int retcode) {
        if (retcode == 0) return "Success";
        return msgdefret.getOrDefault(retcode, "Error");
    }

    public static String retInfo(String locale, int retcode) {
        if (locale == null || locale.isEmpty()) return retInfo(retcode);
        if (retcode == 0) return "Success";
        String key = locale == null ? "" : locale;
        Map<Integer, String> map = msgrets.get(key);
        if (map == null) return "Error";
        return map.getOrDefault(retcode, "Error");
    }

    @Comment("国际化文件配置加载")
    public static void loadProperties() throws MalformedURLException, IOException {
       File file = new File(System.getProperty(RESNAME_APP_CONF, "conf"));
       if (!file.exists() || file.isFile()) return;
       File[] files = file.listFiles();
       for (int i = 0; i < files.length; i++) {
            File codeFile =  files[i];
            String fileName = codeFile.getName();
            if (!fileName.startsWith(MESSAGE_PREFIX)) {
                continue;
            }
            Properties ps = new Properties();
            String local = fileName.substring(MESSAGE_PREFIX.length(), fileName.lastIndexOf("."));
            InputStream stream = codeFile.toURI().toURL().openStream();
            ps.load(stream);
            ps.forEach((x, y) -> { msgrets.computeIfAbsent(local, k -> new HashMap<Integer, String>()).put(Integer.valueOf(x+""), "" + y);});
       }
       
    }
    
    public static void main(String[] args) {
    	MsgConstant constant = new MsgConstant();
    	System.out.println(retInfo("zh_CN", GROUP_UPDATEANNOYTYPE));
    	System.out.println(retInfo("zh_TW", GROUP_UPDATEANNOYTYPE));
    	System.out.println(retInfo("en_US", GROUP_UPDATEANNOYTYPE));
	}
 
}
